/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.validator;

import com.minierp.bean.AreaBean;
import com.minierp.dao.AreaDao;
import com.minierp.model.Area;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class AreaValidator implements Validator {
	private AreaDao areaDao;

	public AreaDao getAreaDao() {
		return this.areaDao;
	}

	public void setAreaDao(AreaDao areaDao) {
		this.areaDao = areaDao;
	}

	public boolean supports(Class clazz) {
		return AreaBean.class.isAssignableFrom(clazz);
	}

	public void validate(Object obj, Errors errors) {
		Area form = ((AreaBean) obj).getArea();
		boolean hasError = false;
		if (form.getAreaName() != null && !form.getAreaName().trim().equals("")) {
			if (form.getParentId() != 0L && form.getId() != 0L) {
				if (form.getParentId() == form.getId()) {
					errors.rejectValue("area.parentId", "", "不能选择自己作为『上级』");
					hasError = true;
				} else {
					Area d = (Area) this.areaDao.get(Area.class,
							form.getParentId());
					if (d != null
							&& d.getParentIdStr() != null
							&& d.getParentIdStr().indexOf(
									";" + form.getId() + ";") != -1) {
						errors.rejectValue("area.parentId", "",
								"不能选择自己的下级作为『上级』");
						hasError = true;
					}
				}
			}
		} else {
			errors.rejectValue("area.areaName", "", "请输入『名称』");
			hasError = true;
		}

		if (!hasError
				&& (form.getSequCode() == null || form.getSequCode().trim()
						.equals(""))) {
			errors.rejectValue("area.sequCode", "", "请输入『序号』");
			hasError = true;
		}

	}
}